package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * 处理品牌相关的请求
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "02. 品牌管理模块")
@Slf4j
@Validated
@RequestMapping("/brands")
@RestController
public class BrandController {

    @Autowired
    private IBrandService brandService;
    public BrandController(){
        log.debug("创建控制器对象: BrandController");
    }
    //http://localhost:8080/add?name=品牌015&pinyin=pinpailinglingyi&logo=picture&description=品牌001简介&keywords=p&sort=155&sales=1000&productCount=120&commentCount=200&positiveCommentCount=10&enable=1
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 1)
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addNew(BrandAddNewDTO brandAddNewDTO){
        log.debug("开始处理[添加品牌]的请求,参数:{}",brandAddNewDTO);
        try {
            brandService.addNew(brandAddNewDTO);
            log.debug("测试添加数据成功!");
            return "添加品牌成功!";
        }catch(ServiceException e){
            String message = e.getMessage();
            log.debug("测试添加数据失败! 品牌名称已经被占用！");
            return message;
        }catch (RuntimeException e) {
            log.debug("添加数据失败！程序运行过程中出现了RuntimeException！");
            return "添加品牌失败！程序运行过程中出现了RuntimeException！";
        } catch (Throwable e) {
            log.debug("添加数据失败！程序运行过程中出现了Throwable！");
            return "添加品牌失败！程序运行过程中出现了Throwable！";
        }
    }
    //http://localhost:8080/brands/9527/delete
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 2)
    @RequestMapping(value = "/{id:[0-9]+}/delete",method = RequestMethod.POST)
    public JsonResult<Void> delete(@Range(min = 1,message = "删除品牌失败，尝试删除的品牌的ID无效！")
                             @PathVariable Long id){
        log.debug("开始处理[根据id删除品牌]的请求,参数: {}", id);
        brandService.delete(id);
        return JsonResult.ok();
    }

//    //http://localhost:9080/brands/9527/update
//    @ApiOperation("根据id修改品牌详情")
//    @ApiOperationSupport(order = 3)
//    @PostMapping( "/update")
//    public JsonResult<Void> updateById(@Valid BrandUpdateDTO brandUpdateDTO){
//        log.debug("开始处理[根据id修改品牌详情]的请求,参数: {}", brandUpdateDTO.getId());
//        brandService.updateById(brandUpdateDTO);
//        return JsonResult.ok();
//    }

    // http://localhost:9080/brands/3/update
    @ApiOperation("修改品牌详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【修改品牌详情】的请求：id={}, brandUpdateDTO={}", id, brandUpdateDTO);
        brandService.updateInfoById(id, brandUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/brands/9527/enable
    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1, message = "启用品牌失败，尝试启用的品牌的ID无效！")
                                      @PathVariable Long id) {
        log.debug("开始处理【启用品牌】的请求，参数：{}", id);
        brandService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:8080/brands/9527/disable
    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@Range(min = 1, message = "禁用品牌失败，尝试禁用的品牌的ID无效！")
                                       @PathVariable Long id) {
        log.debug("开始处理【禁用品牌】的请求，参数：{}", id);
        brandService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/9527
    @ApiOperation("根据id查询品牌详情")
    @ApiOperationSupport(order = 419)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<BrandStandardVO> getStandardById(@Range(min = 1,message = "根据id查询品牌详情失败，尝试查询的品牌的ID无效！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，无参数");
        BrandStandardVO queryResult = brandService.getStandardById(id);
        //List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(queryResult);
    }

    // http://localhost:8080/brands
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list() {
        log.debug("开始处理【查询品牌列表】的请求，无参数");
        List<BrandListItemVO> list = brandService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:8080/brands/hello/delete2
    @ApiOperation("【已过期】根据名称删除品牌")
    @ApiOperationSupport(order = 3)
    @PostMapping("/{name:[a-z]+}/delete2")
    public String delete2(@PathVariable String name) {
        String message = "尝试删除名称值为【" + name + "】的品牌";
        log.debug(message);
        return message;
    }
    // http://localhost:8080/brands/test/delete3
    @ApiOperation("【已过期】测试删除品牌")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "/test/delete3")
    public String delete3() {
        String message = "尝试测试删除品牌";
        log.debug(message);
        return message;
    }

    // http://localhost:9080/brands/cache/rebuild
    @ApiOperation("重建品牌缓存")
    @ApiOperationSupport(order = 600)
    @PostMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache() {
        log.debug("开始处理[重建品牌缓存]的请求, 无参数");
        brandService.rebuildCache();
        return JsonResult.ok();
    }


}
